<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 1.x
|  Copyright © 2005-2008, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* Ineo - Libraries - Category Management
*  
* This library handles the category management functions of Ineo,
* including adding and removing categories, loading category data,
* loading article lists for a category, etc.
*
* @package		Ineo
* @subpackage   Libraries
* @author		Dan Cryer & William McGann
* @copyright	Dan Cryer & William McGann
* @version		1.0.0
*/
class ineo_lib_categories
{
    var $cache = array();
    
    /**
    * Load all categories into an array:
    */
    function load_all_cats( $order = 'category_order, category_name ASC' )
    {
        // Check cache:
        if( isset( $this->cache['load_all_cats'] ) )
        {
            return $this->cache['load_all_cats'];
        }
       
        // Grab data:
        $array = array(
						'select' => '*',
						'from'   => 'ams_categories',
					  );
					  
		$this->ipsclass->DB->simple_construct( $array );
		$this->ipsclass->DB->simple_order( $order );
		$cats = $this->ipsclass->DB->simple_exec();
		
		// Create basic array:
		$categories = array();
		
		// Loop through to build an array:
		while( $res = $this->ipsclass->DB->fetch_row( $cats ) )
		{
		    $categories[$res['category_id']] = $res;
		}
		
		// Cache:
		$this->cache['load_all_cats'] = $categories;
		
		// Return:
		return $categories;
    }
    
    /**
    * Load one specific category and return
    */
    function load_category_data( $cat_id )
    {
        // Check cache:
        if( isset( $this->cache['load_cat_'.$cat_id] ) )
        {
            return $this->cache['load_cat'.$cat_id];
        }
        
        // Query Data:
        $array = array(
    					'select' => 'category_name',
    					'from'   => 'ams_categories',
    					'where'  => 'category_id = ' . $cat_id,
    				  );

    	$this->ipsclass->DB->simple_construct( $array );
    	$q = $this->ipsclass->DB->simple_exec();
    	
    	// Fetch returned row:
    	$cat = $this->ipsclass->DB->fetch_row( $q );
    	
    	// Cache:
		$this->cache['load_cat_'. $cat_id] = $cat;
        
        // Return:
    	return $cat;
    }
    
    /**
    * Load a list of all of the articles inside a category:
    */
    function load_category_articles( $cat_id , $order , $start , $limit )
    {
        // Check cache:
        if( isset( $this->cache['cat_articles_'.$cat_id] ) )
        {
            return $this->cache['cat_articles_'.$cat_id];
        }
        
        // Build the query:
        $array = array(
						'select' => '*',
						'from'   => 'ams_articles',
						'where'  => 'article_cat_id = '. $cat_id . ' AND article_is_queued = 0',
					  );
		$this->ipsclass->DB->simple_construct( $array );
		
		$this->ipsclass->DB->simple_order($order);
		
		// Add the order by:
		// $this->ipsclass->DB->simple_order( 'article_title ASC' );
		
		// Add limit:
		$this->ipsclass->DB->simple_limit( $start, $limit );
		$q = $this->ipsclass->DB->simple_exec();
		
		while( $art = $this->ipsclass->DB->fetch_row($q) )
		{
		    $return[$art['article_id']] = $art;
		}
		
		// Cache:
		$this->cache['cat_articles_'.$cat_id] = $return;
		
		// Return:
		return $return;
    }
    
    /**
    * Count the number of articles in the category:
    */
    function category_count_articles( $cat_id )
    {
        $this->ipsclass->DB->simple_select( 'count(*) as cnt', 'ams_articles', 'article_cat_id = ' . $cat_id );
	    $count = $this->ipsclass->DB->simple_exec();
	    $count = $this->ipsclass->DB->fetch_row( $count );
	    return $count['cnt'];
    }

	/**
	* Load all categories with a specific parent:
	*/
	
	function load_cats_by_parent( $parent_id )
	{
		// Check cache:
        if( isset( $this->cache['load_cats_by_parent_'.$parent_id] ) )
        {
            return $this->cache['load_cats_by_parent_'.$parent_id];
        }
       
        // Grab data:
        $array = array(
						'select' => 'c.* , count( art.article_id ) as category_articles',
						'from'   => array('ams_categories' => 'c'),
						'add_join' => array(1=> array(
												'type' => 'left',
												'from' => array('ams_articles' => 'art'),
												'where' => 'art.article_cat_id = c.category_id and art.article_is_queued = 0',
							)),
						'where'  => 'c.category_parent = ' . $parent_id . ' group by c.category_id'
					  );
					  
		$this->ipsclass->DB->simple_construct( $array );
		$this->ipsclass->DB->simple_order( 'category_order, category_name ASC' );
		$cats = $this->ipsclass->DB->simple_exec();
		
		// Create basic array:
		$categories = array();
		
		if( $this->ipsclass->DB->get_num_rows($cats) == 0 )
		{
			return false;
		}
		
		// Loop through to build an array:
		while( $res = $this->ipsclass->DB->fetch_row( $cats ) )
		{
		    $categories[$res['category_id']] = $res;
		}
		
		// Cache:
		$this->cache['load_cats_by_parent_'.$parent_id] = $categories;
		
		// Return:
		return $categories;
	}
	
	function select_list_items()
	{
	  $this->ipsclass->DB->simple_construct( array(
	                   'select' => 'category_id, category_name, category_parent',
	                   'from' => 'ams_categories' ) );
	  $this->ipsclass->DB->simple_order( 'category_order, category_name ASC' );
    $this->ipsclass->DB->simple_exec();
    
	$array = array();
	$array[0] = array();
    
    while( $row = $this->ipsclass->DB->fetch_row() )
    {
      $array[$row['category_parent']][$row['category_id']] = $row;
    }
        
    $menu .= $this->select_list_items_internal( '0', $array );
    
    return $menu;
	}
	
	function select_list_items_internal( $root_id, $array, $jump_string="", $depth_guide="" )
	{
		$this->depth_guide = '--';
		if ( is_array( $array[ $root_id ] ) )
		{
		      foreach( $array[ $root_id ] as $id => $cat_data )
		      {
		      $jump_string .= "<option value=\"{$cat_data['category_id']}\">&nbsp;|".$depth_guide." ".$cat_data['category_name']."</option>\n";
		      $jump_string = $this->select_list_items_internal( $cat_data['category_id'], $array, $jump_string, $depth_guide . $this->depth_guide );
		      }
		}
		
		return $jump_string;
	}
	
	
	function display_article_list( $catId , $cat , $display_order = 'article_id DESC' , $start = 0 )
	{
		// Setup start / limit point ( for pagination ):
		  $start = ( empty($this->ipsclass->input['st']) ) ? $start : $this->ipsclass->input['st'];
		  $limit = $this->ipsclass->vars['ams_cat_artcount'];
		
		// Load all of the articles in the category:
		$articles = $this->load_category_articles( $catId , $display_order , $start , $limit );
		
		
		// Count number of articles in this category
		$art_count = $this->category_count_articles( $catId );
	  
		// Build pagination links:
	  $links = $this->ipsclass->build_pagelinks( array( 'TOTAL_POSS' => $art_count,
		                                                  'PER_PAGE' => $limit,
		                                                  'CUR_ST_VAL' => $start,
		                                                  'BASE_URL'    => $this->core->base_url.'showcat='
		                                                  . $catId,) );
		
		$list = '';
		
		$return  = '';
		
		if( is_array($articles) )
		{
			// Loop thru articles and build our listing:
			foreach( $articles as $art )
			{
				$art['article_date'] = $this->ipsclass->get_date( $art['article_created_date'] , '' );
				$list .= $this->core->templates['articles']->art_row( $art );
			}
			
			$return .= $this->core->templates['categories']->cat_listing( $cat['category_name'] , $list , $links );
			
			$has_art = true;
		}
		else
		{
			$has_art = false;
		}
		
		if(!$has_cats and !$has_art) // check for feline paintings.
		{
			// output using main category listing template:
		  $return .= $this->core->templates['categories']->cat_listing( $cat['category_name'] , 'none' , 'none' );
		}
		
		return $return;
	}
}

?>